﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;

namespace PrintfServer
{
    public class MyBartender
    {
        BarTender.Application btApp = new BarTender.Application();
        BarTender.Format btFormat;
        public List<String> PrintKey = new List<string>();
        public List<String> PrintValue = new List<string>();
        private String ErrInfo = "";

        //public WebSocket ws = null;
        public String GetErrInfo()
        {
            return ErrInfo;
        }
        public bool BartenderPrint(String Name, List<KeyValuePair<string, string>> PrintParam, String ModouleName)
        {
            PrintKey.Clear();
            try
            {
                String PrintName = "C:\\moudle\\" + ModouleName + ".btw";
                if (!File.Exists(PrintName))
                {
                    ErrInfo = "模板文件不存在";
                    return false;
                }
                btFormat = btApp.Formats.Open(PrintName, false, Name);
                btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;  //设置同序列打印的份数
                btFormat.PrintSetup.NumberSerializedLabels = 1;  //设置需要打印的序列数
                for (int i = 0; i < PrintParam.Count; i++)
                {
                    btFormat.SetNamedSubStringValue(PrintParam[i].Key, PrintParam[i].Value); //向bartender模板传递变量
                }
                btFormat.PrintOut(false, false); //第二个false设置打印时是否跳出打印属性
                btFormat.Close(BarTender.BtSaveOptions.btSaveChanges); //退出时是否保存标签
                return true;
            }
            catch
            {
                ErrInfo = "打印异常";
                return false;
            }
        }


    }
}
